import { toast, modal, getStorage } from "../../../utils/storage"
Page({
  data: {
    id: '',
    entity: {},
    photos: [],
    remark: '',
    attachments: [],
  },
  async onLoad({ id }) {
    let { statusCode, data: { code, data: entity } } = await wx.$http.get("/quality/act/activityById", { act_id: id });
    if (statusCode === 200 && code === 200) {
      let { evaluationTypes: testItem } = entity.students;//考核项
      entity.hasScanCode = testItem.indexOf('1') >= 0 || testItem.indexOf('2') >= 0
      entity.hasImage = testItem.indexOf('3') >= 0;
      entity.hasRemark = testItem.indexOf('4') >= 0;
      entity.hasAttachment = testItem.indexOf('5') >= 0;
      this.setData({ id, entity });
    }
    else {
      this.setData({ entity: null })
      toast({ title: "网络异常" });
    }
  },
  goback() {
    wx.navigateBack();
  },
  scanQrCode() {
    wx.scanCode({
      scanType: "qrCode",
      onlyFromCamera: true,
      success: async ({ result }) => {
        let data = JSON.parse(result);
        let { statusCode, data: { code, message } } = await wx.$http.post("/quality/act/qrScan", data).catch();
        if (statusCode === 200 && code === 200)
          toast({ title: "扫码成功" });
        else
          toast({ title: message });
      },
      fail: (res) => {
        if (res.errMsg.indexOf('cancel') < 0)//如果用户不取消扫码，则说明扫码失败。
          toast({ title: "请使用相机扫一扫二维码" })
      }
    });
  },
  deletePhoto(e) {
    const { index } = e.detail;
    let files = this.data.photos;
    files.splice(index, 1);
    this.setData({ photos: files })
  },
  uploadPhoto(e) {
    const { url, name } = e.detail.file;
    const files = this.data.photos;
    files.push({ url, name: e.detail.name, deletable: true, isImage: true });
    this.setData({ photos: files })
  },
  deleteAttachment() {
    this.setData({ attachments: [] });
  },
  uploadAttachment(e) {
    const { url, name } = e.detail.file;
    this.setData({ attachments: [{ url, name }] });
  },
  async submit() {
    if (!this.data.entity) {
      toast({ title: "无效的活动，无法提交作业" });
      return;
    }
    let { hasImage, hasRemark, hasAttachment } = this.data.entity;
    if (hasImage && this.data.photos.length != 3) {
      toast({ title: "请上传3张活动照片" });
      return;
    }
    let remark = this.data.remark.trim();
    if (hasRemark && remark.trim() === '') {
      toast({ title: "请输入活动总结" });
      return;
    }
    if (hasAttachment && this.data.attachments.length !== 1) {
      toast({ title: "请上传附件文件" });
      return;
    }

    let value = await modal({ content: "提交作业后无法修改，是否继续？" });
    if (!value) return;

    var loaders = [];
    if (hasImage)
      this.data.photos.forEach(x => loaders.push(wx.$http.upload("/quality/studentWork/upload", x.url, "image", {
        formData: {
          actId: this.data.id,
        }
      })));
    if (hasAttachment)
      loaders.push(wx.$http.upload("/quality/studentWork/upload", this.data.attachments[0].url, "zip", {
        formData: {
          actId: this.data.id
        }
      }));

    //上传文件
    let res = [];
    if (loaders.length > 0)
      res = await wx.$http.all(...loaders);
    for (const item of res) {
      if (item.statusCode === 200) {
        if (typeof (item.data) === "string")
          item.data = JSON.parse(item.data);
      }
      else {
        toast({ title: "网络异常，请重试" });
        return;
      }
      if (item.data.code != 200) {
        toast({ title: item.data.message });
        return;
      }
    }

    let params = {
      actId: this.data.id,
      actRemarks: hasRemark ? remark : ''
    };
    if (hasImage) {
      let images = [];
      for (let i = 0; i < 3; i++) {
        if (res[i].statusCode === 200 && res[i].data.code === 200)
          images.push(res[i].data.data.url);
      }
      params.imgUrls = images;
    }
    if (hasAttachment && res[3].statusCode === 200 && res[3].data.code === 200)
      params.zipUrl = res[3].data.data.url;

    let { statusCode, data: { code, message } } = await wx.$http.post("/quality/student/submitWorkForActOfStu", params);
    if (statusCode === 200 && code === 200) {
      toast({ title: "成功提交活动作业" });
      wx.redirectTo({
        url: '/pages/student/homeworkinfo/homeworkinfo?id=' + this.data.id,
      })
    }
    else
      toast({ title: message })
  }
})